package top.codedance.iotp.tools.serverTool.ui;

import com.alibaba.fastjson.JSONObject;
import top.codedance.iotp.common.util.JsonUtil;
import top.codedance.iotp.iot.sdk.PublicStandandService;
import top.codedance.iotp.tools.terminal.TerminalApplication;
import top.codedance.iotp.tools.terminal.ui.WaitLoading;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

/**
 * @author xuedianfu
 */
public class ViewRegistInfomation extends javax.swing.JDialog {
    private MainFrame mf;
    private WaitLoading waitLoading = new WaitLoading(this);

    /**
     * Creates new form ViewRegistInfomation
     */
    public ViewRegistInfomation(MainFrame mf) {
        super(mf, true);
        this.mf = mf;
        initComponents();
        URL url = Thread.currentThread().getContextClassLoader().getResource("logo.gif");
        ImageIcon icon = new ImageIcon(url);
        setIconImage(icon.getImage());
        setMinimumSize(new Dimension(800, 700));
        setLocationRelativeTo(this.mf.getShellFrame());
        setTitle("代码飞舞采控系统-查看注册中心中的服务");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jtx_registUrl = new javax.swing.JTextField();
        jb_view = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jta_result = new javax.swing.JTextArea();
        jb_viewServerInfo = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("注册中心地址：");

        jtx_registUrl.setText("http://127.0.0.1:10014");
        jtx_registUrl.setToolTipText("http://127.0.0.1:10014");

        jb_view.setText("查看服务及其客户端");
        jb_view.setActionCommand("查看服务及其客户端");
        jb_view.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_viewActionPerformed(evt);
            }
        });

        jta_result.setColumns(20);
        jta_result.setRows(5);
        jScrollPane1.setViewportView(jta_result);

        jb_viewServerInfo.setText("查看服务详情");
        jb_viewServerInfo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_viewServerInfoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtx_registUrl, javax.swing.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jb_view)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jb_viewServerInfo)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtx_registUrl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jb_view)
                    .addComponent(jb_viewServerInfo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 479, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jb_viewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_viewActionPerformed
        String url = jtx_registUrl.getText();
        if(!url.trim().equals("")) {
            waitLoading.open();
            TerminalApplication.pool.submit(() -> {
                try {
                    PublicStandandService ss = new PublicStandandService(url);
                    JSONObject response = ss.request("/information", "");
                    jta_result.setText(JsonUtil.getInstance().format(response.toJSONString()));
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(rootPane, "请求体必须是一个可转化为JSON对象的字符串。");
                } finally {
                    waitLoading.close();
                }
            });
        }else{
            JOptionPane.showMessageDialog(rootPane, "请正确输入注册中心地址。");
        }
    }//GEN-LAST:event_jb_viewActionPerformed

    private void jb_viewServerInfoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_viewServerInfoActionPerformed
        String url = jtx_registUrl.getText();
        if(!url.trim().equals("")) {
            waitLoading.open();
            TerminalApplication.pool.submit(() -> {
                try {
                    PublicStandandService ss = new PublicStandandService(url);
                    JSONObject response = ss.request("/query/server/list", "");
                    jta_result.setText(JsonUtil.getInstance().format(response.toJSONString()));
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(rootPane, "请求体必须是一个可转化为JSON对象的字符串。");
                } finally {
                    waitLoading.close();
                }
            });
        }else{
            JOptionPane.showMessageDialog(rootPane, "请正确输入注册中心地址。");
        }
    }//GEN-LAST:event_jb_viewServerInfoActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jb_view;
    private javax.swing.JButton jb_viewServerInfo;
    private javax.swing.JTextArea jta_result;
    private javax.swing.JTextField jtx_registUrl;
    // End of variables declaration//GEN-END:variables
}
